package com.kwshare.system.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.kwshare.common.core.domain.system.Menu;
import com.kwshare.common.core.domain.system.RoleMenu;
import com.kwshare.system.mapper.MenuMapper;
import com.kwshare.system.mapper.RoleMenuMapper;
import com.kwshare.system.service.IMenuService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.LinkedHashMap;
import java.util.List;

/**
 * 菜单接口
 *
 * @author yangjing
 */
@Service
public class MenuServiceImpl implements IMenuService {

    @Resource
    private MenuMapper menuMapper;

    @Resource
    private RoleMenuMapper roleMenuMapper;

    /**
     * 查询菜单列表信息
     *
     * @return 菜单列表
     */
    @Override
    public List<Menu> getMenuList() {
        QueryWrapper<Menu> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("del_flag",0);
        return menuMapper.selectList(queryWrapper);
    }

    /**
     * 查询菜单列表信息
     *
     * @return 菜单列表
     */
    @Override
    public List<Menu> getMenuList(List<Long> roleIds) {
        HashSet<Long> set = new HashSet<>();
        List<Menu> menus = new ArrayList<>();
        for(Long roleId:roleIds){
            QueryWrapper<RoleMenu> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("role_id",roleId);
            List<RoleMenu> list = roleMenuMapper.selectList(queryWrapper);
            for(RoleMenu roleMenu:list){
                set.add(roleMenu.getMenuId());
            }
        }
        for(Long menuId:set){
            QueryWrapper<Menu> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("del_flag",0);
            queryWrapper.eq("id",menuId);
            Menu menu = menuMapper.selectOne(queryWrapper);
            menus.add(menu);
        }
        return menus;
    }

    /**
     * 构建菜单树信息
     * @param menus 菜单列表
     * @return 菜单树
     */
    @Override
    public List<Object> buildMenuTree(List<Menu> menus) {
        List<Object> returnList = new ArrayList<>();
        List<Long> tempList = new ArrayList<>();
        for (Menu menu : menus) {
            tempList.add(menu.getId());
        }

        for (Menu menu : menus) {
            LinkedHashMap<String, Object> map = new LinkedHashMap<>();
            // 父节点
            if(!tempList.contains(menu.getParentId())){
                map.put("path",menu.getPath());
                map.put("name",menu.getName());
                map.put("component",menu.getComponent());
                map.put("redirect",menu.getRedirect());
                LinkedHashMap<String, Object> meta = new LinkedHashMap<>();
                meta.put("title",menu.getName());
                map.put("meta",meta);
                List<Object> childList = new ArrayList<>();
                // 子节点
                for(Menu c : menus){
                    LinkedHashMap<String, Object> child = new LinkedHashMap<>();
                    if(c.getParentId() == menu.getId()){
                        child.put("path",c.getPath());
                        child.put("name",c.getName());
                        child.put("component",c.getComponent());
                        LinkedHashMap<String, Object> childMeta = new LinkedHashMap<>();
                        childMeta.put("title",c.getName());
                        child.put("meta",childMeta);
                        childList.add(child);
                    }
                }
                map.put("children",childList);
                returnList.add(map);
            }
        }
        return returnList;
    }

    /**
     * 构建角色菜单树信息
     * @param menus 菜单列表
     * @return 菜单树
     */
    @Override
    public List<Object> buildRoleMenuTree(List<Menu> menus) {
        List<Object> returnList = new ArrayList<>();
        List<Long> tempList = new ArrayList<>();
        for (Menu menu : menus) {
            tempList.add(menu.getId());
        }
        for (Menu menu : menus) {
            LinkedHashMap<String, Object> map = new LinkedHashMap<>();
            // 父节点
            if(!tempList.contains(menu.getParentId())){
                map.put("id",menu.getId());
                map.put("label",menu.getName());
                List<Object> childList = new ArrayList<>();
                // 子节点
                for(Menu c : menus){
                    LinkedHashMap<String, Object> child = new LinkedHashMap<>();
                    if(c.getParentId() == menu.getId()){
                        child.put("id",c.getId());
                        child.put("label",c.getName());
                        childList.add(child);
                    }
                }
                map.put("children",childList);
                returnList.add(map);
            }
        }
        return returnList;
    }

    @Override
    public List<Long> selectMenuListByRoleId(Long roleId) {
        ArrayList<Long> list = new ArrayList<>();
        QueryWrapper<RoleMenu> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("role_id",roleId);
        queryWrapper.ge("menu_id",100);
        List<RoleMenu> roleMenus = roleMenuMapper.selectList(queryWrapper);
        for(RoleMenu roleMenu : roleMenus){
            list.add(roleMenu.getMenuId());
        }
        return list;
    }
}
